package servlet;

import dao.*;
import pojo.Book;
import pojo.Category;
import pojo.Members;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    protected void userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        Object realcode = session.getAttribute("realcode");
        String username=request.getParameter("username");
        System.out.println(username);
        String password=request.getParameter("password");
        System.out.println(password);
        String code=request.getParameter("code");
        List<Category> categoryMsg = null;
        List<Book> bookSpecaialMsg=null;
        List<Book> bookHotMsg=null;

        try {
            categoryMsg = CategoryDao.getCategoryMsg();
            bookSpecaialMsg= BookSql.querySpecial();
            bookHotMsg=BookSql.queryHot();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        User user= UserSql.Login(username);
        if(user==null){
            request.setAttribute("msg","用户名不存在");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else if(!user.getPwd().equals(password)){
            request.setAttribute("msg","密码错误");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else if(!code.toLowerCase().equals(realcode.toString().toLowerCase())){
            request.setAttribute("msg","验证码错误");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else {
            request.getSession(true).setAttribute("user",user);
            request.setAttribute("username",username);
            try {
                toIndex(request,response);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    protected static void adminLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        Object realcode = session.getAttribute("realcode");
        String adminname=request.getParameter("username");
        System.out.println(adminname);
        String password=request.getParameter("password");
        System.out.println(password);
        String code=request.getParameter("code");
        Members members = null;
        members = AdminSql.adminLogin(adminname);
        String headPortrait=members.getHeadPortrait();
        if(members==null){
            request.setAttribute("msg","管理员不存在");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else if(!members.getPwd().equals(password)){
            request.setAttribute("msg","密码错误");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else if(!code.toLowerCase().equals(realcode.toString().toLowerCase())){
            request.setAttribute("msg","验证码错误");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else {

            request.getSession(true).setAttribute("members",members);
            request.setAttribute("adminname",adminname);
            request.setAttribute("headPortrait",headPortrait);
            request.getRequestDispatcher("iframeUi.jsp").forward(request,response);
        }
    }
    protected static void toIndex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<Category> categoryMsg = null;
        List<Book> bookSpecaialMsg=null;
        List<Book> bookHotMsg=null;
        int num = CartSql.queryCartNum();
        try {
            categoryMsg = CategoryDao.getCategoryMsg();
            bookSpecaialMsg= BookSql.querySpecial();
            bookHotMsg=BookSql.queryHot();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("categoryMsg",categoryMsg);
        request.setAttribute("bookSpecaialMsg",bookSpecaialMsg);
        request.setAttribute("bookHotMsg",bookHotMsg);
        request.setAttribute("num",num);
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    protected static void toHot(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> categoryMsg = null;
        List<Book> bookHotMsg=null;
        int num= 0;
        try {
            categoryMsg = CategoryDao.getCategoryMsg();
            bookHotMsg=BookSql.queryHotAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            num = CartSql.queryCartNum();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.setAttribute("categoryMsg",categoryMsg);
        request.setAttribute("bookHotMsg",bookHotMsg);
        request.setAttribute("num",num);
        request.getRequestDispatcher("hotArea.jsp").forward(request,response);
    }
    protected static void toSpecial(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> categoryMsg = null;
        List<Book> bookSpecaialMsg=null;
        int num = 0;
        try {
            num = CartSql.queryCartNum();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            categoryMsg = CategoryDao.getCategoryMsg();
            bookSpecaialMsg= BookSql.querySpecialAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("categoryMsg",categoryMsg);
        request.setAttribute("num",num);
        request.setAttribute("bookSpecaialMsg",bookSpecaialMsg);
        request.getRequestDispatcher("specialArea.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String m=request.getParameter("m");

        if("userLogin".equals(m)&&request.getParameter("user").equals("user")){
            userLogin(request,response);
        }
        else if("userLogin".equals(m)&&request.getParameter("user").equals("admin")){

                adminLogin(request,response);

        }
        else if("toIndex".equals(m)){
            try {
                toIndex(request,response);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        else if("toHot".equals(m)){
            toHot(request,response);
        }
        else if("toSpecial".equals(m)){
            toSpecial(request,response);
        }

    }
}
